﻿namespace JobButenko.Models.BusinessLogicLayer
{
    using System.Linq;
    using System.Web;

    public class EmployerRepository
    {
        private readonly JobDataContext dataContext;

        public EmployerRepository(JobDataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        public void CreateEmployer(Employer employer)
        {
            var dataManager = new DataManager();
            var user = dataManager.User.GetDbUser(HttpContext.Current.User.Identity.Name);

            var newEmployer = new Employer
                                  {
                                      Adress = employer.Adress,
                                      Email = user.Email,
                                      EmployerName = employer.EmployerName,
                                      Info = employer.Info,
                                      Phone = employer.Phone,
                                      UserId = user.UserId
                                  };
            this.dataContext.Employers.InsertOnSubmit(newEmployer);
            this.dataContext.SubmitChanges();
        }

        public Employer GetEmployerFromUserName(string userName)
        {
            return this.dataContext.Employers.SingleOrDefault(emp => emp.User.UserName.Equals(userName));
        }

        public Employer GetEmployerFromId(int id)
        {
            return this.dataContext.Employers.SingleOrDefault(c => c.EmpoyerId.Equals(id));
        }
    }
}